package com.httpserver.one;

import java.io.IOException;
import java.io.InputStream;

/**
 *@project:HttpServerDemo
 *@author   wuhn
 *@version  1.0
 *@createTime:2017年10月18日 下午12:22:14
 *@description: 接收浏览器发送过来的 URL字符串
 */
public class Receive {
	
	InputStream  in=null;

	public Receive(InputStream in) {
		super();
		this.in = in;
	}
	
	
	/**
	 * 该方法作用是将URL请求的文件返回
	 */
	public String parse(){
		StringBuffer  receiveStr =new StringBuffer(2048);
		int i;
		byte[] buffer =new byte[2048];
		try {
			i = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			i=-1;
		}	
		for (int j = 0; j < i; j++) {
			receiveStr.append((char)buffer[j]); //将取得的信息，存到 receiveStr变量中
		}
		return getUri(receiveStr.toString());
	}


	private String getUri(String receiveStr) {
		 int index1 , index2;
		 index1 =receiveStr.indexOf(' ');
		 if(index1 != -1){
			 index2 = receiveStr.indexOf(' ',index1+1);
			 if(index2 > index1 ){
				 return receiveStr.substring(index1+1, index2);
			 }
		 }
		return null;
	}
	
}
